package com.it.jasypt.controller;

import com.it.jasypt.dto.UserDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/users")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/testSer")
    public ResponseEntity<UserDTO> testSer() {
        UserDTO user = new UserDTO();
        user.setId(1L);
        user.setUsername("Jasypt");
        user.setPassword("Jasypt");
        user.setEmail("jasypt@gmail.com");
        user.setIdCard("340000200010100001");
        user.setPhone("18812341234");
        user.setIdCard2(user.getIdCard());
        user.setIdCard3(user.getIdCard());
        user.setIdCard4(user.getIdCard());
        return ResponseEntity.ok(user);
    }
    
    @PostMapping
    public ResponseEntity<Long> createUser(@RequestBody UserDTO userDTO) {
        Long userId = userService.createUser(userDTO);
        return ResponseEntity.ok(userId);
    }

    @GetMapping("/{id}")
    public ResponseEntity<UserDTO> getUser(@PathVariable Long id) {
        UserDTO user = userService.getUserById(id);
        return ResponseEntity.ok(user);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Void> updateUser(@PathVariable Long id, @RequestBody UserDTO userDTO) {
        userDTO.setId(id);
        userService.updateUser(userDTO);
        return ResponseEntity.ok().build();
    }
}